package project;

import java.util.ArrayList;

public class SelectionMethod2Demo {

	static ArrayList<String[]> frequencytbl = new ArrayList<String[]>();
	static String[] c1 = new String[] { "c1,1", "c2,1", "c3,1", "c4,1" };
	static String[] c2 = new String[] { "c1,2", "c2,2" };
	static String[] c3 = new String[] { "c1,3", "c2,3", "c3,3", "c4,3", "c5,3" };
	static String[] c4 = new String[] { "c1,4", "c2,4", "c3,4" };

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		frequencytbl.add(c1);
		frequencytbl.add(c2);
		frequencytbl.add(c3);
		frequencytbl.add(c4);

		System.out.println("demo");
		for (String[] arr : frequencytbl) {
			for (int i = 0; i < arr.length; i++) {
				System.out.print(arr[i] + " ");
			}
			System.out.println();
		}
		System.out.println();
		int k = 4;
		System.out.println("k=" + k);
		int attr = frequencytbl.size();
		System.out.println("attr=" + attr);
		// initial first attr
		System.out.println("\nassigning most freq for modes at begining\n");
		String[][] Modes = new String[k][attr];

		if (k <= attr) {
			for (int i = 0; i < k; i++) {
				Modes[i][i] = frequencytbl.get(i)[0];
			}
			for (int i = 0; i < Modes.length; i++) {
				for (int j = 0; j < Modes[0].length; j++) {
					System.out.print(Modes[i][j] + " ");
				}
				System.out.println();
			}

			for (int c = 0; c < k; c++) {
				if (c == 0) {
					int x = c;
					int y = 0;
					boolean down = true;
					for (int i = 0; i < frequencytbl.size(); i++) {
						if (i != x) {
							if (down) {
								if (frequencytbl.get(i).length == 1) {
									Modes[x][i] = frequencytbl.get(i)[0];
									y = 0;
								} else
									for (int j = 0; j < frequencytbl.get(i).length; j++) {
										if (!checkconflict(
												frequencytbl.get(i)[j], Modes,
												i)
												&& (j > y)) {
											Modes[x][i] = frequencytbl.get(i)[j];
											y = j;
											break;
										}
									}
								// }
								if (Modes[x][i] == null) {
									down = false;
								}
							}
							if (!down) { // up
								if (frequencytbl.get(i).length == 1) {
									Modes[x][i] = frequencytbl.get(i)[0];
									y = 0;
								} else
									for (int j = 0; j < frequencytbl.get(i).length; j++) {
										if (!checkconflict(
												frequencytbl.get(i)[j], Modes,
												i)
												&& (j < y)) {
											System.out.println("I am here!!");
											Modes[x][i] = frequencytbl.get(i)[j];
											y = j;
										}
									}
								if (frequencytbl.get(attr - 1)[0] != null) {
									down = true;
								}
							}
						}
					}
				}
				if ((0 < c) && (c < k - 1)) {
					int x = c;
					int y = 0;
					boolean down = true;
					for (int i = c; i >= 0; i--) {
						if (i != x) {
							if (down) { // / down
								if (frequencytbl.get(i).length == 1) {
									Modes[x][i] = frequencytbl.get(i)[0];
									y = 0;
								} else
									for (int j = 0; j < frequencytbl.get(i).length; j++) {
										if (!checkconflict(
												frequencytbl.get(i)[j], Modes,
												c)
												&& (j > y)) {
											Modes[x][i] = frequencytbl.get(i)[j];
											y = j;
											break;
										}
									}
								if (Modes[x][i] == null) {
									down = false;
								}
							}
							if (!down) { // up
								if (frequencytbl.get(i).length == 1) {
									Modes[x][i] = frequencytbl.get(i)[0];
									y = 0;
								} else
									for (int j = 0; j < frequencytbl.get(i).length; j++) {
										if (!checkconflict(
												frequencytbl.get(i)[j], Modes,
												c)
												&& (j < y)) {
											System.out.println("I am here!!");
											Modes[x][i] = frequencytbl.get(i)[j];
											y = j;
											// break;
										}
									}
								if (frequencytbl.get(attr - 1)[0] != null) {
									down = true;
								}
							}
						}
					}
					// ///// right
					for (int i = c; i < frequencytbl.size(); i++) {
						if (i != x) {
							if (down) { // / down
								if (frequencytbl.get(i).length == 1) {
									Modes[x][i] = frequencytbl.get(i)[0];
									y = 0;
								} else
									for (int j = 0; j < frequencytbl.get(i).length; j++) {
										if (!checkconflict(
												frequencytbl.get(i)[j], Modes,
												c)
												&& (j >= y)) {

											Modes[x][i] = frequencytbl.get(i)[j];
											y = j;
											break;
										}
									}
								// }

								if (Modes[x][i] == null) {
									down = false;
								}
							}
							if (!down) { // up
								if (frequencytbl.get(i).length == 1) {
									Modes[x][i] = frequencytbl.get(i)[0];
									y = 0;
								} else
									for (int j = 0; j < frequencytbl.get(i).length; j++) {
										if (!checkconflict(
												frequencytbl.get(i)[j], Modes,
												c)
												&& (j < y)) {
											System.out.println("I am here!!");
											Modes[x][i] = frequencytbl.get(i)[j];
											y = j;
											// break;
										}
									}
								if (frequencytbl.get(attr - 1)[0] != null) {
									down = true;
								}
							}
						}
					}
				}

				if (c == k - 1) {
					int x = c;
					int y = 0;
					boolean down = true;
					// /////left
					for (int i = c; i >= 0; i--) {
						if (i != x) {
							if (down) { // / down
								if (frequencytbl.get(i).length == 1) {
									Modes[x][i] = frequencytbl.get(i)[0];
									y = 0;
								} else
									for (int j = 0; j < frequencytbl.get(i).length; j++) {
										if (!checkconflict(
												frequencytbl.get(i)[j], Modes,
												c)
												&& (j > y)) {
											Modes[x][i] = frequencytbl.get(i)[j];
											y = j;
											break;
										}
									}
								if (Modes[x][i] == null) {
									down = false;
								}
							}
							if (!down) { // up
								if (frequencytbl.get(i).length == 1) {
									Modes[x][i] = frequencytbl.get(i)[0];
									y = 0;
								} else
									for (int j = 0; j < frequencytbl.get(i).length; j++) {
										if (!checkconflict(
												frequencytbl.get(i)[j], Modes,
												c)
												&& (j < y)) {
											System.out.println("I am here!!");
											Modes[x][i] = frequencytbl.get(i)[j];
											y = j;
											// break;
										}
									}
								if (frequencytbl.get(attr - 1)[0] != null) {
									down = true;
								}
							}
						}
					}
					// ///// right
					for (int i = c; i < frequencytbl.size(); i++) {
						if (i != x) {
							if (down) { // / down
								if (frequencytbl.get(i).length == 1) {
									Modes[x][i] = frequencytbl.get(i)[0];
									y = 0;
								} else
									for (int j = 0; j < frequencytbl.get(i).length; j++) {
										if (!checkconflict(
												frequencytbl.get(i)[j], Modes,
												c)
												&& (j >= y)) {

											Modes[x][i] = frequencytbl.get(i)[j];
											y = j;
											break;
										}
									}
								// }

								if (Modes[x][i] == null) {
									down = false;
								}
							}
							if (!down) { // up
								if (frequencytbl.get(i).length == 1) {
									Modes[x][i] = frequencytbl.get(i)[0];
									y = 0;
								} else
									for (int j = 0; j < frequencytbl.get(i).length; j++) {
										if (!checkconflict(
												frequencytbl.get(i)[j], Modes,
												c)
												&& (j < y)) {
											System.out.println("I am here!!");
											Modes[x][i] = frequencytbl.get(i)[j];
											y = j;
											// break;
										}
									}
								if (frequencytbl.get(attr - 1)[0] != null) {
									down = true;
								}
							}
						}
					}
				}
			}
			System.out.println();
			for (int z = 0; z < Modes.length; z++) {
				for (int j = 0; j < Modes[0].length; j++) {
					System.out.print(Modes[z][j] + " ");
				}
				System.out.println();
			}
		}

		if (k > attr) {
			int counter = 0;
			for (int i = 0; i < k; i++) {
				if (counter > attr - 1) {
					counter = 0;
				}
				if (counter <= attr - 1) {
					Modes[i][counter] = frequencytbl.get(counter)[0];
					counter++;
				}
			}
			for (int i = 0; i < Modes.length; i++) {
				for (int j = 0; j < Modes[0].length; j++) {
					System.out.print(Modes[i][j] + " ");
				}
				System.out.println();
			}

			for (int c = 0; c < k; c++) {
				// ////////
				int x = c;
				int y = 0;
				boolean down = true;
				for (int i = 0; i < frequencytbl.size(); i++) {
					if (i != x) {
						if (down) { // / down
							if (frequencytbl.get(i).length == 1) {
								Modes[x][i] = frequencytbl.get(i)[0];
								y = 0;
							} else
								for (int j = 0; j < frequencytbl.get(i).length; j++) {
									if (!checkconflict(frequencytbl.get(i)[j],
											Modes, i) && (j > y)) {

										Modes[x][i] = frequencytbl.get(i)[j];
										y = j;
										break;
									}
								}
							// }

							if (Modes[x][i] == null) {
								down = false;
							}
						}
						if (!down) { // up
							if (frequencytbl.get(i).length == 1) {
								Modes[x][i] = frequencytbl.get(i)[0];
								y = 0;
							} else if (!checkconflictall(frequencytbl.get(i),
									Modes, i)) {
								for (int j = frequencytbl.get(i).length - 1; j > 0; j--) {
									if (!checkconflict(frequencytbl.get(i)[j],
											Modes, i) && (j < y)) {
										System.out.println("I am here!!");
										Modes[x][i] = frequencytbl.get(i)[j];
										y = j;
									}

								}
							} else {
								for (int j = frequencytbl.get(i).length - 1; j > 0; j--) {
									if ((j <= y)) {
										System.out.println("I am here!!");
										Modes[x][i] = frequencytbl.get(i)[j];
										y = j;
									}
								}
							}
							if (frequencytbl.get(attr - 1)[0] != null) {
								down = true;
							}
						}
					}

				}
			}

			System.out.println();
			for (int z = 0; z < Modes.length; z++) {
				for (int j = 0; j < Modes[0].length; j++) {
					System.out.print(Modes[z][j] + " ");
				}
				System.out.println();
			}
		}
	}

	public static boolean checkconflict(String str, String[][] Modes, int col) {
		for (int i = 0; i < Modes.length; i++) {
			if ((Modes[i][col] != null) && Modes[i][col].equals(str)) {
				return true;
			}
		}
		return false;
	}

	public static boolean checkconflictall(String[] str, String[][] Modes,
			int col) {
		boolean bool = false;
		for (int i = 0; i < str.length; i++) {
			if (checkconflict(str[i], Modes, col)) {
				bool = true;
			}
		}
		for (int i = 0; i < Modes.length; i++) {
			if ((Modes[i][col] == null)) {
				bool = true;
			}
		}
		return bool;
	}

}
